var http = require('http');
var url = require('url');
var querystring = require('querystring');

function start(route,handle){ //传进一个路由函数
	function onRequest (req,res) {
		var pathname = url.parse(req.url).pathname;
		console.log(pathname);
/*		if (pathname == "/start"){
			res.writeHead(200, {"Content-Type":"text/html"});
			res.write("This is start page");
			res.end();
		}
		if (pathname == "/upload"){
			res.writeHead(200, {"Content-Type":"text/html"});
			res.write("This is upload page");
			res.end();
		}*/
		
		// 上面就是相当于一个路由转发的功能，下面将添加路由转发模块
		
/*		var pageContent = route(pathname,handle);
		console.log(pageContent);

		res.writeHead(200, {"Content-Type":"text/html"});
		res.write(pageContent);
		res.end();*/
		//	通过传内容的方式将pageContent传到response对象中，下面将直接将response对象传过来

//		route(pathname , handle , res);

		//	下面获取Post请求数据，交给路由器
		var postData = "";
		req.on('data', function (chunk) {		
			postData += chunk;
			console.log("收到chunk数据：" + chunk);
		});
		req.on('end',function () {
			console.log("请求数据结束");
			route(pathname ,handle , res , postData);
		});
	}
	http.createServer(onRequest).listen(3000);
	console.log("Server is started at port 3000");
}

exports.start = start;